package own.stu.jobgib.playown.redis.redisson;

import org.redisson.Redisson;
import org.redisson.api.RHyperLogLog;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HyperLogLogTest {

    public static void main(String[] args) throws InterruptedException {
        Config config = new Config();
        config.useSingleServer()
                .setConnectionMinimumIdleSize(4)
                .setAddress("redis://localhost:6379"); // 修改hosts文件
        RedissonClient client = Redisson.create(config);

        RHyperLogLog<Integer> log = client.getHyperLogLog("uv");

        ExecutorService service = Executors.newFixedThreadPool(5);
        Set<Integer> set =new HashSet<>();
        CountDownLatch latch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            service.submit(() -> {

                for (int j = 0; j < 200; j++) {
                    List<Integer> list = new ArrayList<>();
                    for (int k = 0; k < 100; k++) {
                        int v = finalI * 20_000 + j * 100 + k;
                        list.add(v);
                    }
                    log.addAll(list);
                    set.addAll(list);
                }
                latch.countDown();
            });
        }
        System.out.println(" --------------" + 10 * 200 * 100);
        latch.await();
        System.out.println(log.count() + "  ----- " + set.size());
        service.shutdown();
        client.shutdown();
    }
}
